/**
 * 
 */
package de.sinnlosimnetz.forum.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import de.sinnlosimnetz.forum.database.Database;

/**
 * @author Henning D
 *
 */
public class SinMySqlConnector extends SinAbstractConnector
{
	private Connection connection = null;
	
	public SinMySqlConnector()
	{
		super();
	}

	public SinMySqlConnector(SinConnectorEnum dbms, String dbUrl, int dbPort,
			String dbName, String dbUser, String dbPass)
	{
		super(dbms, dbUrl, dbPort, dbName, dbUser, dbPass);
	}

	public SinMySqlConnector(Database database)
	{
		super(database);
	}

	/* (non-Javadoc)
	 * @see de.sinnlosimnetz.forum.util.SinAbstractConnector#closeConnection()
	 */
	@Override
	public void closeConnection() throws SQLException, Exception
	{
		connection.close();
	}

	/* (non-Javadoc)
	 * @see de.sinnlosimnetz.forum.util.SinAbstractConnector#getConnection()
	 */
	@Override
	public Connection getConnection()
	{
		if (connection == null)
		{
			try
			{
				Class.forName("com.mysql.jdbc.Driver").newInstance();
				connection = DriverManager.getConnection("jdbc:mysql://" + getDbUrl() + ":" + getDbPort() + "/"
						+ getDbName(), getDbUser(), getDbPass());
			}
			catch (InstantiationException e)
			{
				e.printStackTrace();
			}
			catch (IllegalAccessException e)
			{
				e.printStackTrace();
			}
			catch (ClassNotFoundException e)
			{
				e.printStackTrace();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		return connection;
	}

}
